#include <gtk/gtk.h>
#include <stdlib.h>

struct status{
        int number;
        int count;
        } radio;

static void destroy(GtkWidget* w, gpointer* d)
{
        gtk_main_quit();
}

static void radio_play(GtkWidget* widget, gpointer* data)
{
        system("mpc play 1");
}

static void radio_pause(GtkWidget* widget, gpointer* data)
{
        static gboolean pause;
        if (pause == FALSE)
                system("mpc stop");
        if (pause == TRUE)
                system("mpc play 1");
        pause = !pause;
}

static void radio_next(GtkWidget* widget, gpointer* data)
{
        radio.number++;
        if (radio.number > radio.count)
                radio.number = 1;
        char cmd[32];
        sprintf(cmd,"mpc play %i",radio.number);
        system("mpc stop");
        system(cmd);
}

static void radio_previous(GtkWidget* widget, gpointer* data)
{
        radio.number--;
        if (radio.number < 1)
                radio.number = radio.count;
        char cmd[32];
        sprintf(cmd,"mpc play %i",radio.number);
        system("mpc stop");
        system(cmd);
}

static void radio_add(GtkWidget* widget, gpointer* data)
{
        system("amixer set PCM 4dB+");
}

static void radio_remove(GtkWidget* widget, gpointer* data)
{
        system("amixer set PCM 4dB-");
}

static void radio_init(int argc, char* argv[])
{
        
        char* cmd = NULL;
        int c;
        radio.number = 1;
        radio.count = 0;
        system("rm /tmp/mpc_playlist");
        system("amixer set PCM 82%");
        system("mpc clear");
        if (argv[1] != NULL){
                cmd = g_strjoin(" ","mpc load",argv[1],NULL);
        } else {
                cmd = "mpc load radiostation";
        }
        system(cmd);

        /* count playlist elements */
        system("mpc playlist > /tmp/mpc_playlist");
        FILE *file = fopen("/tmp/mpc_playlist","r");
        do {
                c = fgetc(file);
                if (c == '\n') 
                        radio.count++;
        } while (c != EOF);
        fclose(file);
        if (radio.count == 0){
                g_print("Sorry, no valid playlist found.");
                exit(1);
        }

        system("mpc play 1");
}

int main(int argc, char* argv[])
{
        radio_init(argc, argv);
        gtk_init(&argc, &argv);
        GtkToolItem* item;
        GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        GtkWidget* toolbar = gtk_toolbar_new();

        item = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
        gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);
        g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(radio_play),NULL);

        item = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PAUSE);
        gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);
        g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(radio_pause),NULL);

        item = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PREVIOUS);
        gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);
        g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(radio_previous),NULL);

        item = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_NEXT);
        gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);
        g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(radio_next),NULL);

        item = gtk_tool_button_new_from_stock(GTK_STOCK_ADD);
        gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);
        g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(radio_add),NULL);

        item = gtk_tool_button_new_from_stock(GTK_STOCK_REMOVE);
        gtk_toolbar_insert(GTK_TOOLBAR(toolbar),item,-1);
        g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(radio_remove),NULL);

        gtk_container_add(GTK_CONTAINER(window),toolbar);

        gtk_window_set_default_size(GTK_WINDOW(window),220,32);
        gtk_widget_grab_focus(GTK_WIDGET(window));

        gtk_widget_show_all(window);
        g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);
        gtk_main();
        return 0;
}

